/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LayoutManager;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.uml2.uml.Extension;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.ExtensionRequiredEditPart;
import com.cea.papyrus.core.editpart.connection.NamedGraphEdgeEditPart;
import com.cea.papyrus.core.figures.RelationEndpointLocator;
import com.cea.papyrus.core.figures.connection.SolidLineFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller of the {@link Extension} link.
 * 
 * @author Patrick Tessier
 */
public class ExtensionEditPart extends NamedGraphEdgeEditPart implements PropertyChangeListener {

	/**
	 * 
	 */
	protected ExtensionRequiredEditPart requiredLabel;

	/**
	 * 
	 * 
	 * @param graphEdge 
	 * @param modelManager 
	 */
	public ExtensionEditPart(GraphEdge graphEdge,ModelManager modelManager) {
		super(graphEdge,modelManager);
		requiredLabel =new ExtensionRequiredEditPart((GraphNode)getGraphEdge().getContained().get(1),this.getUmlElement(),modelManager);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#getUmlElement()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public Extension getUmlElement() {
		return (Extension)((Uml1SemanticModelBridge)(getGraphEdge()).getSemanticModel()).getElement();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#getCommand(org.eclipse.gef.Request)
	 */
	//@Override
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	public Command getCommand(Request request) {
		// command should be transmetted to contained parts
		CompoundCommand command = new CompoundCommand();
		command.add(requiredLabel.getCommand(request));
		command.add(super.getCommand(request));
		return command;
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#activate()
	 */
	/**
	 * 
	 */
	@Override
	public void activate() {
		if (!isActive()) {
			super.activate();

			//add listener on extension end
			if (getUmlElement().getMemberEnds().size()!=0 &&getUmlElement().getMemberEnds().get(0)!=null) {
				((Notifier)getUmlElement().getMemberEnds().get(0)).eAdapters().add(_adapter);
				if (((Property)getUmlElement().getMemberEnds().get(0)).getLowerValue()!=null) {
					((Notifier)((Property)getUmlElement().getMemberEnds().get(0)).getLowerValue()).eAdapters().add(_adapter);
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#deactivate()
	 */
	/**
	 * 
	 */
	@Override
	public void deactivate() {
		if (isActive()) {
			// remove listener on extension end
			
			if (getUmlElement().getMemberEnds().size()!=0 &&getUmlElement().getMemberEnds().get(0)!=null) {
				((Notifier)getUmlElement().getMemberEnds().get(0)).eAdapters().remove(_adapter);
				if (((Property)getUmlElement().getMemberEnds().get(0)).getLowerValue()!=null) {
					((Notifier)((Property)getUmlElement().getMemberEnds().get(0)).getLowerValue()).eAdapters().remove(_adapter);
				}
			}
			super.deactivate();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected IFigure createFigure() {
		return new SolidLineFigure(ColorConstants.black, ColorConstants.black, 1);
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public SolidLineFigure getFigure() {
		return (SolidLineFigure)super.getFigure();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.edge.NamedGraphEdgeEditPart#createChild(java.lang.Object)
	 */
	/**
	 * 
	 * 
	 * @param model 
	 * 
	 * @return 
	 */
	@Override
	protected EditPart createChild(Object model) {
		if( model instanceof GraphNode) {
			// index in contained edge = 1
			GraphNode element = (GraphNode)getModelChildren().get(1);	// getModelChildren only returns GraphNodes (0 = name, 1 = required)
			if((((GraphNode)model) == ((GraphNode)element)) ) {
				return requiredLabel;
			}
		}
		return super.createChild(model);
	}

	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		requiredLabel.refresh();
		addLabel(this.getContentPane().getLayoutManager(), requiredLabel);

	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#propertyChange(java.beans.PropertyChangeEvent)
	 */
	/**
	 * 
	 * 
	 * @param event 
	 */
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		//refreshVisuals();
		refreshBendpoints();
	}

	/**
	 * to display a label at teh end of the link.
	 * 
	 * @param layoutManager of the current display
	 * @param theStringEditPart of the label
	 */
	protected void addLabel(LayoutManager layoutManager,ExtensionRequiredEditPart theStringEditPart){

		layoutManager.setConstraint(theStringEditPart.getFigure(),new RelationEndpointLocator(
				this.getFigure(),
				false,
				theStringEditPart.getGraphNode().getPosition())); 
	}

}